package com.farm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.farm.dao.FairDao;
import com.farm.dao.UserDao;
import com.farm.service.FairService;
import com.farm.utils_all.HUtil;
import com.farm.utils_all.XVDateUtils;
@Service("FairService")
public class FairServiceImpl implements FairService {
	@Autowired
	FairDao fairDao;
	@Autowired
	UserDao userDao;
	
	static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	@Override
	public List<Map<String, Object>> selectAllFair() {
		List<Map<String, Object>> selectAllFair = fairDao.selectAllFair();
		for (Map<String, Object> map : selectAllFair) {
			map.put("stime",XVDateUtils.MillisFormat(Integer.valueOf(map.get("f_start_time").toString())));
			map.put("etime", XVDateUtils.MillisFormat(Integer.valueOf(map.get("f_end_time").toString())));
		}
		return selectAllFair;
	}
	@Override
	public List<Map<String, Object>> selectAllBanner() {
		List<Map<String, Object>> selectAllBanner = fairDao.selectAllBanner();
		return selectAllBanner;
	}
	@Override
	public Map<String, Object> addQd(Map<String, Object> map) {
		//先查询用户是否已经绑定过
		Map<String, Object> userDetail = userDao.selectUserDetail(HUtil.putInMap("openid",map.get("openid")));
		if(userDetail != null && !map.get("u_phone").equals(userDetail.get("u_phone")) && !map.get("u_name").equals(userDetail.get("u_name"))) {
			return HUtil.putInMap("code",0,"msg","一个微信只能绑定一个用户，您已经绑定 "+userDetail.get("u_name")+" 用户了");
		}
		//查询用户是否参加
		Map<String, Object> userByFair = fairDao.selectUserByFair(map);
		if(userByFair == null) {
			return HUtil.putInMap("code",1,"msg","您未参加该会议");
		}
		//绑定openid
		int user = userDao.updateUser(HUtil.putInMap("openid",map.get("openid"),"u_id",userByFair.get("u_id")));
		if(user <= 0) {
			return HUtil.putInMap("code",2,"msg","绑定微信失败");
		}
		//进行签到
		if(Integer.valueOf(userByFair.get("sign_in_status").toString())== 1) {
			map.putAll(userByFair);
			return HUtil.putInMap("code",1001,"msg","该会议已签到,不可重复签到","map",map);
		}
		
		int qd = fairDao.updateQD(HUtil.putInMap("si_id",userByFair.get("si_id"),"u_name",map.get("u_name"),"time",System.currentTimeMillis()/1000));
		if(qd<=0) {
			return HUtil.putInMap("code",2,"msg","签到失败,请联系客服");
		}	
		map.put("si_id", userByFair.get("si_id"));
		return HUtil.putInMap("code",200,"msg","签到成功","map",map);
	}
	@Override
	public List<Map<String, Object>> MyQDFair(Map<String, Object> maps) {
		List<Map<String, Object>> myQDFair = fairDao.MyQDFair(maps);
		for (Map<String, Object> map : myQDFair) {
			map.put("stime", XVDateUtils.MillisFormat(Integer.valueOf(map.get("f_start_time").toString())));
			map.put("etime", XVDateUtils.MillisFormat(Integer.valueOf(map.get("f_end_time").toString())));
			map.put("time", XVDateUtils.MillisFormat(Integer.valueOf(map.get("sign_in_time").toString())));
		}
		return myQDFair;
	}
	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(XVDateUtils.MillisFormat(1624613306));
	}
	@Override
	public Map<String, Object> MyQDFairDetail(Map<String, Object> map) {
		Map<String, Object> myQDFairDetail = fairDao.MyQDFairDetail(map);
		if(myQDFairDetail != null) {
			myQDFairDetail.put("stime", XVDateUtils.MillisFormat(Integer.valueOf(myQDFairDetail.get("f_start_time").toString())));
			myQDFairDetail.put("etime", XVDateUtils.MillisFormat(Integer.valueOf(myQDFairDetail.get("f_end_time").toString())));
			myQDFairDetail.put("time", XVDateUtils.MillisFormat(Integer.valueOf(myQDFairDetail.get("sign_in_time").toString())));
		}
		return myQDFairDetail;
	}
	@Override
	public Map<String, Object> FPRoom(Map<String, Object> map) {
		//是否有同行人员,查询他们权限
		Map<String, Object> userDetail = fairDao.selectRZTrue(HUtil.putInMap("u_id",map.get("u_id"),"f_id",map.get("f_id")));
		if(userDetail == null) {
			return HUtil.putInMap("code",3,"msg","您未参加该会议,无法办理入住");
		}
		Map<String, Object> userDetailTX = new HashMap<String, Object>();
		Map<String, Object> selectHotelForTX = new HashMap<String, Object>();
		if(map.get("u_phone") != null && !"".equals(map.get("u_phone"))) {
			if(userDetail.get("u_phone") != null && !"".equals(userDetail.get("u_phone"))) {
				if(userDetail.get("u_phone").equals(map.get("u_phone")))
					return HUtil.putInMap("code",7,"msg","同行人员手机号与您手机号相同,无法办理入住");
			}
			userDetailTX = userDao.selectUserByPhoneDetail(HUtil.putInMap("u_phone",map.get("u_phone")));
			if(userDetailTX == null) {
				return HUtil.putInMap("code",3,"msg","同行人员未参加该会议,无法办理入住");
			}		
			selectHotelForTX = fairDao.selectHotelForUser(HUtil.putInMap("u_id",userDetailTX.get("u_id")));
			if (selectHotelForTX != null) {
				return HUtil.putInMap("code",1,"msg","同行人员已办理入住,无法重复入住");
			}
		}
		//入住酒店1.先查询是否已入住
		Map<String, Object> selectHotelForUser = fairDao.selectHotelForUser(HUtil.putInMap("u_id",map.get("u_id")));
		if (selectHotelForUser != null) {
			return HUtil.putInMap("code",1,"msg","您已办理入住,无法重复入住");
		}
		//未入住,2.查询这场会议入住哪家酒店
		List<Map<String, Object>> selectHotelForFair = fairDao.selectHotelForFair(HUtil.putInMap("f_id",map.get("f_id")));
		if(selectHotelForFair == null) {
			return HUtil.putInMap("code",2,"msg","这场会议不支持办理入住");
		}
		//选择酒店的房间
		int count = 1,counts = 1;
		if(userDetailTX != null && selectHotelForTX == null) {
			count = 0;counts = 2;
		}
		List<String> cx_list = new ArrayList<String>();
		Set<Integer> cx_list1 = new HashSet<Integer>();
		for (Map<String, Object> string : selectHotelForFair) {
			cx_list1.add(Integer.valueOf(string.get("hrn_id").toString()));
			cx_list.add(string.get("hrn_id").toString());
		}
		System.out.println(cx_list);
		//先查询符合各种属性的房间
		Map<String, Object> selectRoom = fairDao.selectRoom(HUtil.putInMap("hr_sex",userDetail.get("u_sex"),"hr_type",userDetail.get("u_type"),
				"hr_status",count,"hrn_id",cx_list1/*selectHotelForFair.get("hrn_id")*/));
		//没有房间查询空房间
		int room = 0;
		if(selectRoom == null) {
			selectRoom = fairDao.selectRoomNull(HUtil.putInMap("hrn_id",cx_list1));
			if(selectRoom == null) {
				return HUtil.putInMap("code",4,"msg","没有符合条件的房间");
			}
			room = 1;
		}
		
		
		//查询床位
		List<Map<String, Object>> selectRoomBed = fairDao.selectRoomBed(HUtil.putInMap("hr_id",selectRoom.get("hr_id")));
		if(selectRoomBed.size()<counts) {
			return HUtil.putInMap("code",6,"msg","床位不够");
		}
		
		System.out.println(selectRoom.get("hr_status"));
		int hr_status = Integer.valueOf(selectRoom.get("hr_status").toString())+counts;
		System.out.println(hr_status);
		int updateRZType = 0;
		if(room == 0) {
			updateRZType = fairDao.updateRZType(HUtil.putInMap("hr_id",selectRoom.get("hr_id"),"hr_status",hr_status));
		}else {
			updateRZType = fairDao.updateRZAllType(HUtil.putInMap("hr_sex",userDetail.get("u_sex"),"hr_type",userDetail.get("u_type"),"hr_id",selectRoom.get("hr_id"),"hr_status",hr_status));
		}
		if(updateRZType <= 0) {
			return HUtil.putInMap("code",5,"msg","没有符合条件的房间");
		}
		//进行分配
		int addRZRoom = fairDao.addRZRoom(HUtil.putInMap("hb_id",selectRoomBed.get(0).get("hb_id"), "hr_id",selectRoom.get("hr_id"),
				"buh_addtime",System.currentTimeMillis()/1000,"u_id",map.get("u_id")));
		if(addRZRoom >0) {
			userDao.updateUserRZ(HUtil.putInMap("u_id",map.get("u_id")));
			fairDao.updateBedStatus(HUtil.putInMap("hb_id",selectRoomBed.get(0).get("hb_id")));
		}
		if(counts>1) {
			//如果报名该会议进行签到,没有报名就只入住
			Map<String, Object> txQD = fairDao.selectRZTrue(HUtil.putInMap("u_id",userDetailTX.get("u_id"),"f_id",map.get("f_id")));
			if(txQD != null) {
				fairDao.updateQD(HUtil.putInMap("si_id",txQD.get("si_id"),"u_name",txQD.get("u_name"),"time",System.currentTimeMillis()/1000));
			}
			int addRZRoom2 = fairDao.addRZRoom(HUtil.putInMap("hb_id",selectRoomBed.get(1).get("hb_id"), "hr_id",selectRoom.get("hr_id"),
					"buh_addtime",System.currentTimeMillis()/1000,"u_id",userDetailTX.get("u_id")));
			if(addRZRoom2>0) {
				userDao.updateUserRZ(HUtil.putInMap("u_id",userDetailTX.get("u_id")));
				fairDao.updateBedStatus(HUtil.putInMap("hb_id",selectRoomBed.get(1).get("hb_id")));
			}
		}
		return HUtil.putInMap("code",200,"msg","办理成功,即将刷新");
	}	

}
